import { ref } from "vue";
export function useTableDelete(deleteApi, getTableList) {
    const loading = ref(false);
    const deletes = async (requestData) => {
        confirmFn(() => deleteFn(requestData));
    };
    async function confirmFn(callBack) {
        const res = await ElMessageBox.confirm("确定删除吗?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消"
        }).catch((err) => {});
        if (res) callBack();
    }
    function deleteFn(requestData) {
        loading.value = true;
        deleteApi(requestData)
            .then((res) => {
                loading.value = false;
                if (res.code === 200) {
                    ElMessage({
                        message: "删除成功",
                        type: "success"
                    });
                    getTableList();
                }
            })
            .catch((err) => (loading.value = true));
    }
    return {
        deletes,
        loading
    };
}

export function useTableOperate(api, config) {
    const type = ref(1);
    const popLoading = ref(false);
    const isShowPopup = ref(false);
    const popupConfig = ref([]);
    function setHandler(callBack) {
        type.value = 2;
        callBack();
        popupConfig.value = config.popupFormSetConfig;
        isShowPopup.value = true;
    }
    function getHandler(callBack) {
        type.value = 3;
        callBack();
        popupConfig.value = config.popupFormGetConfig;
        isShowPopup.value = true;
    }
    function addHandler(callBack) {
        type.value = 1;
        callBack && callBack();
        popupConfig.value = config.popupFormSetConfig;
        isShowPopup.value = true;
    }
    function popupConfrim(formRef, commonListRef, formData) {
        console.log(formData);
        if (type.value === 3) {
            isShowPopup.value = false;
            return;
        }
        const fn = type.value === 1 ? api.addApi : api.setApi;
        formRef.ruleFormRef.validate(async (isValid) => {
            if (isValid) {
                popLoading.value = true;
                const res = await fn(formData).catch((err) => (popLoading.value = false));
                popLoading.value = false;
                if (res.code === 200) {
                    ElMessage({
                        message: type.value === 1 ? "添加成功" : "修改成功",
                        type: "success"
                    });
                    FormData.id = 0;
                    isShowPopup.value = false;
                    commonListRef.tableRef.getTableList();
                }
            }
        });
    }
    function popupClose(formData) {
        isShowPopup.value = false;
        if (!isShowPopup.value) {
            Object.keys(formData).forEach((key) => {
                let type = typeof formData[key];
                if (Array.isArray(formData[key])) type = "array";
                switch (type) {
                    case "array":
                        formData[key] = [];
                        break;
                    case "object":
                        formData[key] = {};
                        break;
                    default:
                        formData[key] = "";
                        break;
                }
            });
        }
    }
    return { popupClose, popupConfrim, type, popLoading, isShowPopup, popupConfig, getHandler, setHandler, addHandler };
}
